package com.ruoyi.zsyh.controller.applet.agent;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zsyh.domain.ZsyhServiceOrder;
import com.ruoyi.zsyh.domain.ZsyhServiceOrderCommission;
import com.ruoyi.zsyh.domain.ZsyhServiceSettle;
import com.ruoyi.zsyh.domain.ZsyhUserRepairstore;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 服务订单
 */
@Controller
@RequestMapping("/applet/zsyh/agentServiceOrder")
public class ZsyhAgentServiceOrderAppletController extends BaseController
{

    @Autowired
    private IZsyhServiceTypeService zsyhServiceTypeService;

    @Autowired
    private IZsyhUserAddressService zsyhUserAddressService;

    @Autowired
    private IZsyhServiceOrderService zsyhServiceOrderService;

    @Autowired
    private IZsyhServiceSettleService zsyhServiceSettleService;

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    @Autowired
    private IZsyhServiceOrderCommissionService zsyhServiceOrderCommissionService;

    /**
     * 会长个人中心 服务订单信息
     * @return
     */
    @RequestMapping("/getServiceSettle")
    @ResponseBody
    public AjaxResult getServiceSettle(String agentId)throws Exception
    {
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }
        String dateString = DateUtils.getDate();
        Integer year = Integer.valueOf(dateString.substring(0,4));
        Integer month = Integer.valueOf(dateString.substring(5,7));
        ZsyhServiceSettle zsyhServiceSettle = new ZsyhServiceSettle();
        zsyhServiceSettle.setAgentId(agentId);
        zsyhServiceSettle.setPeriodYear(year);
        zsyhServiceSettle.setPeriodMonth(month);
        List<ZsyhServiceSettle> list = zsyhServiceSettleService.selectZsyhServiceSettleList(zsyhServiceSettle);
        String agentMoney = "0";//本月收益
        String allMoney = "0";//历史结算
        int orderNum = 0;//共多少维修单
        if(list.size() > 0){
            ZsyhServiceSettle settle = list.get(0);
            agentMoney = settle.getAgentSettleMoney().toString();
        }
        Map allMoneyMap = zsyhServiceSettleService.getMoneyAll(agentId, null, "2");
        if(allMoneyMap != null){
            allMoney = allMoneyMap.get("settleMoney").toString();
        }
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setAgentId(agentId);
        Map map = new HashedMap();
        map.put("orderState", "未取消");
        order.setParams(map);
        List<ZsyhServiceOrder> orderList = zsyhServiceOrderService.selectZsyhServiceOrderList(order);
        orderNum = orderList.size();
        map = new HashedMap();
        map.put("agentMoney", agentMoney);
        map.put("allMoney", allMoney);
        map.put("orderNum", orderNum);
        return success("成功", map);
    }


    /**
     * 会长个人中心 本月收益详情
     * @param type 1今日 2本月 3 3个月 4 6个月
     * @return
     */
    @RequestMapping("/getServiceSettleDetails")
    @ResponseBody
    public AjaxResult getServiceSettleDetails(String agentId, String areaId, String type)throws Exception
    {
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }
        String dateString = DateUtils.getDate();
        Integer year = Integer.valueOf(dateString.substring(0,4));
        Integer month = Integer.valueOf(dateString.substring(5,7));
        ZsyhServiceSettle zsyhServiceSettle = new ZsyhServiceSettle();
        zsyhServiceSettle.setAgentId(agentId);
        zsyhServiceSettle.setPeriodYear(year);
        zsyhServiceSettle.setPeriodMonth(month);
        List<ZsyhServiceSettle> list = zsyhServiceSettleService.selectZsyhServiceSettleList(zsyhServiceSettle);
        String agentMoney = "0";//本月收益
        String allMoney = "0";//总收益

        if(list.size() > 0){
            ZsyhServiceSettle settle = list.get(0);
            agentMoney = settle.getAgentSettleMoney().toString();
        }
        Map allMoneyMap = zsyhServiceSettleService.getMoneyAll(agentId, null, null);
        if(allMoneyMap != null){
            allMoney = allMoneyMap.get("settleMoney").toString();
        }
        String endDate = DateUtils.getDate();
        String startDate = "";
        switch (type){
            case "1": startDate = endDate;break;
            case "2": startDate = DateUtils.getMonthFirstDay();break;
            case "3": startDate = DateUtils.getLeftMonth(-3);break;
            case "4": startDate = DateUtils.getLeftMonth(-6);break;
        }
        if(EmptyUtils.isNotEmpty(startDate) && EmptyUtils.isNotEmpty(endDate)){
            startDate += " 00:00:00";
            endDate += " 23:59:59";
        }
        Map map = new HashedMap();
        ZsyhServiceOrderCommission commission = new ZsyhServiceOrderCommission();
        commission.setOrderAreaId(areaId);
        commission.setAgentId(agentId);
        map.put("beginCreateTime", startDate);
        map.put("endCreateTime", endDate);
        commission.setParams(map);
        List<ZsyhServiceOrderCommission> commissionList = zsyhServiceOrderCommissionService.selectZsyhServiceOrderCommissionList(commission);

        map = new HashedMap();
        map.put("agentMoney", agentMoney);
        map.put("allMoney", allMoney);
        map.put("commissionList", commissionList);
        return success("成功", map);
    }

    /**
     * 历史结算
     * @param agentId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getServiceSettleHistory")
    @ResponseBody
    public AjaxResult getServiceSettleHistory(String agentId)throws Exception
    {
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }
        ZsyhServiceSettle zsyhServiceSettle = new ZsyhServiceSettle();
        zsyhServiceSettle.setAgentId(agentId);
        zsyhServiceSettle.setSettleStatue("2");
        String endDate = DateUtils.getDate();
        String startDate = DateUtils.getLeftYear(-1);
        if(EmptyUtils.isNotEmpty(startDate) && EmptyUtils.isNotEmpty(endDate)){
            startDate += " 00:00:00";
            endDate += " 23:59:59";
        }
        Map map = new HashedMap();
        map.put("beginCreateTime", startDate);
        map.put("endCreateTime", endDate);
        zsyhServiceSettle.setParams(map);
        List<ZsyhServiceSettle> list = zsyhServiceSettleService.selectZsyhServiceSettleList(zsyhServiceSettle);
        return success("成功", list);
    }


    /**
     * 获取服务订单列表
     * @return
     */
    @RequestMapping("/getStoreServiceOrderListByagentId")
    @ResponseBody
    public AjaxResult getStoreServiceOrderListByagentId(String agentId, String pageNum, String pageSize)throws Exception
    {
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }

        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setAgentId(agentId);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("orderState", "未取消");
        order.setParams(map);
        List<ZsyhServiceOrder> orderList = zsyhServiceOrderService.selectZsyhServiceOrderList(order);
        return success("成功", orderList);
    }


}
